import ctypes
import os
import pygame
import datetime
from moon_painter import Painter
from moon_settings import Config
from moon_calendar import Calendar
from moon_utils import *

class Moon():
    def __init__(self):
        self.init_dependencies()
        self.conf = Config()
        self.font = pygame.font.Font(self.conf.font_name,
                                     self.conf.font_size)

        self.back = pygame.image.load(self.conf.background)
        if self.conf.foreground:
            self.forw = pygame.image.load(self.conf.foreground)

        self.info_lineno = 0


    def init_dependencies(self):
        pygame.font.init()

    def generate_wallpaper(self):
        self.moon = pygame.image.load(os.path.join(self.conf.moon_path,
                                                   self.get_image_name()))
        
        self.wallpaper = self.back.copy()
        self.wallpaper.blit(self.moon, self.conf.moon_pos)

    def set_phase(self, phase = -1):
        if phase == -1:
            self.phase = Calendar(datetime.date.today()).get_phase()
        else:
            self.phase = phase

        if self.phase <= 50:
            self.luminosity = self.phase * 2
        else:
            self.luminosity = (99 - self.phase) * 2

    def write_wallpaper(self, dest = "wallpaper.png"):
        pygame.image.save(self.wallpaper, dest)

    def write_info(self, key, value, lineno = -1):
        if lineno == -1:
            lineno = self.info_lineno
            self.info_lineno += 1
        #f = self.font.render(key, 1, self.conf.font_color)
        f = textOutline(self.font, key, self.conf.font_color, (0,0,0))
        self.wallpaper.blit(f, (self.conf.info_pos[0],
                                self.conf.info_pos[1] + lineno*20
                                ))

        #f = self.font.render(value, 1, self.conf.font_color)
        f = textOutline(self.font, value, self.conf.font_color, (0,0,0))
        self.wallpaper.blit(f, (self.conf.info_pos[0] + 100,
                                self.conf.info_pos[1] + lineno*20
                                ))

    def get_phase_name(self, phase = -1):
        if phase == -1:
            phase = self.phase

        phase = int(phase) % 99
        phase_names = ["New Moon", "Waxing Crescent", "First Quarter",
                       "Waxing Gibbous", "Full Moon", "Waning Gibbous",
                       "Third Quarter", "Waning Crescent"]        
        phase_intervals = [(0, 0), (1, 23), (24, 26), (27, 48), 
                           (49,51), (52, 73), (74, 76), (77, 98)]

        p = 0
        for i in phase_intervals:
            if i[0] <= phase and phase <= i[1]:
                return phase_names[p]
            p += 1


    def add_default_info(self):
        self.write_info("luminosity:", str(self.luminosity)+"%")
        self.write_info("phase:", self.get_phase_name())

    def get_image_name(self):
        # force a beautiful full moon
        if 49 <= self.phase <=51 :
            image = "50"
        else:
            image = str((int)(self.phase))

        if int(image) < 10:
            image = "0" + image
        return image + ".png"
    

    def set_wallpaper(self, wallpaper = ""):
        if (wallpaper == ""):
            wallpaper = self.wallpaper
        if os.name == "nt":
            ctypes.windll.user32.SystemParametersInfoA(20, 0, wallpaper, 0)

if __name__ == "__main__":
    date = datetime.datetime.now()
    m = Moon()
    m.set_phase(Calendar(date).get_phase())
    m.generate_wallpaper()
    m.add_default_info()
    m.write_info("date: ", date.strftime("%d %m %Y"))
    m.write_wallpaper("wpp2.bmp")
    m.set_wallpaper("wpp2.bmp")
